package com.monitor.dao.impl;

import java.util.List;

import org.hibernate.SessionFactory;

import com.monitor.dao.StatisticsDao;
import com.monitor.po.Statistics;

@SuppressWarnings("unchecked")
public class StatisticsDaoImpl implements StatisticsDao {
	private SessionFactory sessionFactory;

	@Override
	public Statistics getStatisticsByUid(long uid) throws Exception {
		try {
			List<Statistics> statistics = sessionFactory.getCurrentSession().createQuery("from Statistics s where s.ownerUid=:uid").setLong("uid", uid).list();
			if (statistics != null && !statistics.isEmpty()) {
				return statistics.get(0);
			}
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("exception occured while excuting getStatisticsByUid...");
		}
	}

	@Override
	public void addStatistics(Statistics statistics) throws Exception {
		try {
			sessionFactory.getCurrentSession().persist(statistics);
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("exception occured while excuting addStatistics...");
		}
	}

	@Override
	public void updateStatistics(Statistics statistics) throws Exception {
		try {
			sessionFactory.getCurrentSession().update(statistics);
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("exception occured while excuting updateStatistics...");
		}
	}

	@Override
	public void clearSentSmsCount() throws Exception {
		try {
			sessionFactory.getCurrentSession().createQuery("update Statistics s set s.sentSmsCount=0").executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("exception occured while excuting clearSentSmsCount...");
		}
	}

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

}
